package com.hnxxxy.jfmgr.controller;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.annotation.LogAnno;
import com.hnxxxy.jfmgr.pojo.Jf_conf;
import com.hnxxxy.jfmgr.pojo.Jf_detail;
import com.hnxxxy.jfmgr.service.JfBackstageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author：张浩东
 * @Phone：15096207164
 * @QQ：1367048164
 * @E-mail：1367048164@qq.com
 * @date:2020-05-19 16:33
 */

@RequestMapping("/jf")
@RestController
@Slf4j
public class JfBackstageController {

    @Autowired
    JfBackstageService jfBackstageService;

    /**
     * 查询父类加分信息
     * @return 父类加分信息
     */
    @RequestMapping("/f/jfConfFatherInfo")
    @LogAnno(description = "查询父类加分信息")
    public AjaxRtnEntity findAllJfConfFatherInfo(){
        List<Jf_conf> dictList= jfBackstageService.findAllJfConfFatherInfo();
        AjaxRtnEntity ajaxRtnEntity=new AjaxRtnEntity(1,"",dictList);
        return ajaxRtnEntity;
    }

    /**
     * 根据父id查询子类加分信息
     * @param id  父id
     * @return 有子类返回子类加分信息，没有子类加分信息返回分数（有计算公式会返回计算公式）
     */
    @RequestMapping("/s/jfConfSonInfo")
    @LogAnno(description = "根据父id查询子类加分信息")
    public AjaxRtnEntity queryJfConfSonInfo(@RequestParam("productid") int id){
        AjaxRtnEntity rtnEntity;
        List<Jf_conf> list = jfBackstageService.queryJfConfSonInfo(id);

        if (list.size() > 0){
            log.info("-->请求方法：queryJfConfSonInfo有下级，返回下级");
            rtnEntity = new AjaxRtnEntity(1,"ok",list);
        }else{
            Jf_conf jf_conf = jfBackstageService.queryJfConfPresentInfo(id);
            list.add(jf_conf);

            int x1 =jf_conf.getScoretype();

            if(x1 == 0){
                log.info("-->请求方法：queryJfConfSonInfo没有下级，返回分数");
                rtnEntity = new AjaxRtnEntity(2,"ok",list);
            }else {
                log.info("-->请求方法：queryJfConfSonInfo没有下级，但是有计算公式，返回计算公式");
                rtnEntity = new AjaxRtnEntity(3,"ok",list);
            }
        }
        return rtnEntity;
    }

    /**
     * 证书信息录入
     * @param map 证书信息
     * @return 操作成功或失败的JSON
     */
    @RequestMapping("/i/jfDetailCertificateInfo")
    @LogAnno(description = "证书信息录入",descriptiontype = "insertJfDetailCertificateInfo")
    public AjaxRtnEntity insertJfDetailCertificateInfo(@RequestBody Map<String, String> map){
        Jf_detail jf_detail =  new Jf_detail();
        if(Integer.parseInt(map.get("judge")) == 0){
            jf_detail.Jf_detail1(Float.parseFloat(map.get("grade")),
                    Integer.parseInt(map.get("certificate_type")),
                    Integer.parseInt(map.get("certificate_type_pid")),
                    map.get("date"),
                    map.get("certificate_explain"),
                    map.get("detailed_information"),
                    map.get("certificate_name"),
                    map.get("certificate_type_name"));
        }else {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("n", Float.parseFloat(map.get("numerical_interval")));
            String formula = map.get("scoreformula");
            Object result = convertToCode(formula,map1);
            String x = String.valueOf(result);

            jf_detail.Jf_detail2(
                    Float.valueOf(x),
                    Integer.parseInt(map.get("certificate_type")),
                    Integer.parseInt(map.get("certificate_type_pid")),
                    map.get("date"),
                    map.get("certificate_explain"),
                    Float.parseFloat(map.get("numerical_interval")),
                    map.get("detailed_information"),
                    map.get("certificate_name"),
                    map.get("certificate_type_name"));
        }
        int x = jfBackstageService.insertJfDetailCertificateInfo(jf_detail,map.get("certificate_image"));
        AjaxRtnEntity rtnEntity = new AjaxRtnEntity(x,"ok",null);
        return rtnEntity;
    }

    /**
     * java将字符串转换成可执行代码 工具类
     *
     * @param jexlExp
     * @param map
     * @return
     */
    private static Object convertToCode(String jexlExp, Map<String, Object> map) {
        JexlEngine jexl = new JexlEngine();
        Expression e = jexl.createExpression(jexlExp);
        JexlContext jc = new MapContext();
        for (String key : map.keySet()) {
            jc.set(key, map.get(key));
        }
        if (null == e.evaluate(jc)) {
            return "";
        }
        return e.evaluate(jc);
    }
}
